<?php

include "../dao/corder.php";

class Corder_Dao {
	
	var $corderdetail = array();
	var $order_id;
	var $query;
	var $error;
    var $db;
	
	public function __construct(Database $db){
        $this->db = $db;
    }

	function get_corder($id=null,$posisi=null, $batas=null){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('corder')){
		
			if($id!=null AND $posisi==null AND $batas==null ){
			
				/*$this->query = "SELECT
								*
								FROM 
								corder
								WHERE 
								order_id='".$id."'
								";*/
				$this->query = "SELECT 
								corder.order_id AS order_id, 
								corder.nomor_po AS nomor_po, 
								corder.order_date AS order_date, 
								corder.customer AS customer,
								corder.payment AS payment,
								corder.ship_addr AS ship_addr, 
								corder.shipping AS shipping,
								SUM(order_detail.total) AS total,
								corder.status AS status
								FROM corder INNER JOIN order_detail ON 
								corder.order_id = order_detail.order_id 
								WHERE order_detail.store='".$id."' 
								GROUP BY corder.order_id";				
			}else if($id!=null AND !is_null($posisi) AND !is_null($batas)){
                /*$this->query = "SELECT
								*
								FROM
								corder
								WHERE 
								order_id = '".$id."'
							
								
								LIMIT $posisi,$batas";*/
				$this->query = "SELECT 
								corder.order_id AS order_id, 
								corder.nomor_po AS nomor_po, 
								corder.order_date AS order_date, 
								corder.customer AS customer,
								corder.payment AS payment,
								corder.ship_addr AS ship_addr, 
								corder.shipping AS shipping,
								SUM(order_detail.total) AS total,
								corder.status AS status
								FROM corder INNER JOIN order_detail ON 
								corder.order_id = order_detail.order_id 
								WHERE order_detail.store='".$id."' 
								GROUP BY corder.order_id
								LIMIT $posisi,$batas";
            }else if(!is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
								*
								FROM
								corder               
							
								LIMIT $posisi,$batas";
            }else{
                $this->query = "SELECT
								*
								FROM
								corder      
								";
            }
						
			$result = mysql_query($this->query);
			
			if($result){
				
				while($r=mysql_fetch_array($result)){
				
					$order = new Corder();
					
					$order->order_id = $r['order_id'];
					$order->nomor_po = $r['nomor_po'];
					$order->order_date = $r['order_date'];
					$order->customer = $r['customer'];
					$order->payment= $r['payment'];
					$order->ship_addr= $r['ship_addr'];
					$order->shipping= $r['shipping'];
					$order->total = $r['total'];
					$order->status= $r['status'];
					
					$this->order[] = $order;
					
				}
				
				$this->db->connect->disconnect();
				return $this->order;
				
			}else{
				
				$this->error = 'data tidak ditemukan';
				return $this->error;
				
			}
		}else{
			
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	}
	
	function del($id){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('corder')){
		
			if($id!=null){
			
				$this->query = "DELETE 
								FROM 
								corder 
								WHERE 
								order_id='".$id."'
								";
			
			}else{
			
				$this->error = 'data tidak ditemukan';
				return $this->error;
				
			}
			
			mysql_query($this->query);
            $this->db->connect->disconnect();
		
		}else{
		
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	}
	
	function update($id,$orderstat){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('corder')){
		//echo "test";
			if($id!=null AND $orderstat!=null){
			
				//get_corder();
				
				//if($orderstat=='1'){
				if($orderstat==4){
					$orderstat =$orderstat;
				}else{
					$orderstat = $orderstat+1;
				}
			
					$this->query = "UPDATE
									corder
									SET 
									status=$orderstat
									WHERE
									order_id='".$id."'
									";
				//}
					//print_r($this->query);						
			}else{
			
				$this->error = 'data tidak ditemukan';
				return $this->error;
			
			}
			
			mysql_query($this->query);
            $this->db->connect->disconnect();
		
		}else{
		
			$this->error = 'data tidak ditemukan';
			return $this->error;
			
		}
	
	}
	
}
?>